package vn.student.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.ViewDateSchedule;
import vn.student.mylib.Assignment;
import vn.student.mylib.Class;
import vn.student.mylib.Constants;
import vn.student.mylib.MyCalendarId;
import vn.student.mylib.MyDate;
import vn.student.mylib.ScheduleDay;
import vn.student.mylib.WeekDay;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewScheduleController implements View.OnClickListener{
	Activity act = null;
	AStudentDB myDB = null;
	Date currentViewDate = null;
	public ViewScheduleController(Activity act){
		this.act = act;
		myDB = new AStudentDB(act);
		
		currentViewDate = new Date();
	}
	public void viewNextMonth(){
		if(currentViewDate.getMonth()==11){
			currentViewDate.setYear(currentViewDate.getYear()+1);
			currentViewDate.setMonth(0);
		}
		else{
			currentViewDate.setMonth(currentViewDate.getMonth()+1);
		}
		showSchedule();
	}
	public void viewPrevMonth(){
		if(currentViewDate.getMonth()==0){
			currentViewDate.setYear(currentViewDate.getYear()-1);
			currentViewDate.setMonth(11);
		}
		else{
			currentViewDate.setMonth(currentViewDate.getMonth()-1);
		}
		showSchedule();
	}
	public void showSchedule() {
		showSchedule(MyDate.getDateString(currentViewDate));
	}
	public void showSchedule(String getDateStr){
		LinearLayout lnl = (LinearLayout)act.findViewById(R.id.lnl_main);
		lnl.removeAllViews();
		LayoutInflater inflater=act.getLayoutInflater();
   	 	inflater.inflate(R.layout.schedule, lnl);
   	 	RelativeLayout btn = (RelativeLayout) act.findViewById(R.id.next);
		btn.setClickable(true);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				viewNextMonth();
				
			}
		});
		btn = (RelativeLayout) act.findViewById(R.id.prev);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				viewPrevMonth();
			}
		});
   	 	TextView txtView1 = (TextView) act.findViewById(R.id.month_name);
   	 	txtView1.setText(getDateStr.substring(0, 7));
		ArrayList<Class> classList = myDB.getClassOnDate(getDateStr);
		ArrayList<Assignment> assignmentList = myDB.getAssignmentOnMonthList(getDateStr);
		ArrayList<ScheduleDay> calendar = new ArrayList<ScheduleDay>();
		Date getDate = MyDate.getDate(getDateStr);
		this.currentViewDate = getDate;
		Calendar tmpCalendar = Calendar.getInstance();
		tmpCalendar.set(getDate.getYear(),getDate.getMonth(),getDate.getDate());
		int dayNum = tmpCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		int i =0;
		for(i =0;i<dayNum;i++){
			calendar.add(new ScheduleDay());
		}
		int numClass = classList.size();
		for(i=0;i<numClass;i++){
			Class classInfo = classList.get(i);
			if(classInfo.repeat.equals("0")){
				
				Date tmp = MyDate.getDate(classInfo.dateStart);
				calendar.get(tmp.getDate()-1).countClass++;
				
			}
			else if(classInfo.repeat.equals("1")){
				int weekDayRepeat = WeekDay.getWeekDay(classInfo.dateRepeat);
				int dayBegin = getFirstWeekDayOfMonth(getDate, weekDayRepeat);
				Date dateStart = MyDate.getDate(classInfo.dateStart);
				Date dateEnd = MyDate.getDate(classInfo.dateEnd);
				Date currentDate = (Date) getDate.clone();
				currentDate.setDate(dayBegin);			
				for(int j=dayBegin;j<=dayNum;j+=7){
					currentDate.setDate(j);
					if((currentDate.compareTo(dateStart)>=0&&currentDate.compareTo(dateEnd)<=0)||compare2Date(currentDate,dateStart)){
						calendar.get(j-1).countClass++;
					}
				}
			}
			else if(classInfo.repeat.equals("2")){
				int dayBegin = getFirstWeekDayOfMonth(getDate, 1);
				Date dateStart = MyDate.getDate(classInfo.dateStart);
				Date dateEnd = MyDate.getDate(classInfo.dateEnd);
				Date currentDate = (Date) getDate.clone();
				currentDate.setDate(dayBegin);
				for(int j=dayBegin;j<dayNum;j++){
					currentDate.setDate(j);
					for(;currentDate.getDay()<5&&j<dayNum;j++){
						currentDate.setDate(j);
						if((currentDate.compareTo(dateStart)>=0&&currentDate.compareTo(dateEnd)<=0)||compare2Date(currentDate,dateStart)){
							calendar.get(j-1).countClass++;
						}
					}
					j++;
				}
			}
		}
		for(i=0;i<assignmentList.size();i++){
			Assignment assInfo = assignmentList.get(i);
			Date tmp = MyDate.getDate(assInfo.dueDate);
			calendar.get(tmp.getDate()-1).countAss++;
		}
		getDate.setDate(1);
		int dayBegin = getDate.getDay(); 
		for(i=0;i<dayNum;i++){
			int rlId =  MyCalendarId.id[dayBegin+i];
			calendar.get(i).rlId = rlId;
			RelativeLayout rl = (RelativeLayout) act.findViewById(rlId);
			TextView txtView = (TextView) rl.findViewById(R.id.dateLabel);
			txtView.setText(String.valueOf(i+1));
			int countClass = calendar.get(i).countClass;
			int countAss = calendar.get(i).countAss;
			if(countClass>0||countAss>0){
				RelativeLayout rl3 = (RelativeLayout) rl.findViewById(R.id.detail);
				inflater=act.getLayoutInflater();
				RelativeLayout rl2 = (RelativeLayout) inflater.inflate(R.layout.calendar_row, null);
		    	if(countAss==0)
		    	{
		    		rl2.findViewById(R.id.countAssLabel).setVisibility(View.INVISIBLE);
		    		rl2.findViewById(R.id.assIcon).setVisibility(View.INVISIBLE);
		    	}
		    	else 
		    		((TextView) rl2.findViewById(R.id.countAssLabel)).setText(String.valueOf(countAss));
		    	
		    	if(countClass==0)
		    	{
		    		rl2.findViewById(R.id.countClassLabel).setVisibility(View.INVISIBLE);
		    		rl2.findViewById(R.id.classIcon).setVisibility(View.INVISIBLE);
		    	}
		    	else
		    		((TextView) rl2.findViewById(R.id.countClassLabel)).setText(String.valueOf(countClass));
		    	rl.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						TextView txtView = (TextView) v.findViewById(R.id.dateLabel);
						Date tmp = (Date) currentViewDate.clone();
						tmp.setDate(Integer.valueOf(txtView.getText().toString()));
						viewDateSchedule(MyDate.getDateString(tmp));
						
					}
				});
		    	rl3.addView(rl2);
			}
		}
	}
	private boolean compare2Date(Date date1,Date date2){
		if(date1.getDate()==date2.getDate()&&date1.getMonth()==date2.getMonth()&&date1.getYear()==date2.getYear())
			return true;
		return false;
	}
	private void viewDateSchedule(String date){
		Intent intent = new Intent(act,ViewDateSchedule.class);
		Bundle bundle = new Bundle();
		bundle.putString("date", date);
		intent.putExtras(bundle);
		act.startActivityForResult(intent, Constants.ViewDateScheduleRequestCode);
	}
	private int getFirstWeekDayOfMonth(Date date,int weekDay){
		Date tmpDate = (Date) date.clone();
		tmpDate.setDate(1);
		do{
			if(tmpDate.getDay()==weekDay)
				return tmpDate.getDate();
			else
				tmpDate.setDate(tmpDate.getDate()+1);
		}while(true);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		viewNextMonth();
	}
}
